package com.irdstudio.apicenter.console.dm;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import org.springframework.stereotype.Component;

import com.irdstudio.apicenter.frame.core.jdbc.util.ConnectionManager;

@Component(value = "appTblUserDao")
public class AppTblUserDao {

	/**
	 * 根据主键查询单条记录
	 * 
	 * @param
	 * @return
	 * @throws SQLException
	 */
	public AppTblUser queryWithKeys(String id) throws SQLException {
		AppTblUser appTblUser = null;
		Connection conn = ConnectionManager.getConnection();
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			ps = conn.prepareStatement("SELECT * FROM  tbl_user WHERE id=?");
			ps.setString(1, id);
			rs = ps.executeQuery();
			if (rs.next()) {
				appTblUser = new AppTblUser();
				appTblUser.setId(rs.getString("id"));
				appTblUser.setLogin_name(rs.getString("login_name"));
				appTblUser.setPassword(rs.getString("password"));
				appTblUser.setName(rs.getString("name"));
				appTblUser.setEmail(rs.getString("email"));
				appTblUser.setRemark(rs.getString("remark"));
				appTblUser.setIs_admin(rs.getString("is_admin"));
				appTblUser.setIs_developer(rs.getString("is_developer"));
				appTblUser.setIs_configer(rs.getString("is_configer"));
				appTblUser.setStatus(rs.getString("status"));
			}
		} catch (SQLException e) {
			throw new SQLException("query tbl_user WithKeys is Wrong!"
					+ e.getMessage());
		} finally {
			close2(rs, conn, ps);
		}
		return appTblUser;
	}

	/**
	 * 插入单条记录
	 * 
	 * @param
	 * @return
	 * @throws SQLException
	 */
	public int save(AppTblUser appTblUser) throws SQLException {
		Connection conn = ConnectionManager.getConnection();
		PreparedStatement ps = null;
		int flag;
		try {
			String sql = "insert into tbl_user(id, login_name, password, name, email, remark, is_admin, is_developer, is_configer, status) "
					+ " values(?,?,?,?,?,?,?,?,?,?)";
			ps = conn.prepareStatement(sql);
			ps.setString(1, appTblUser.getId());
			ps.setString(2, appTblUser.getLogin_name());
			ps.setString(3, appTblUser.getPassword());
			ps.setString(4, appTblUser.getName());
			ps.setString(5, appTblUser.getEmail());
			ps.setString(6, appTblUser.getRemark());
			ps.setString(7, appTblUser.getIs_admin());
			ps.setString(8, appTblUser.getIs_developer());
			ps.setString(9, appTblUser.getIs_configer());
			ps.setString(10, "1");
			flag = ps.executeUpdate();
		} catch (SQLException e) {
			throw new SQLException("insert tbl_user is Wrong!" + e.getMessage());
		} finally {
			close1(conn, ps);
		}
		return flag;
	}

	/**
	 * 修改记录
	 * 
	 * @param
	 * @return
	 * @throws SQLException
	 * @throws ClassNotFoundException
	 */

	public int update(AppTblUser appTblUser) throws SQLException,
			ClassNotFoundException {
		Connection conn = ConnectionManager.getConnection();
		PreparedStatement ps = null;
		int flag;
		try {
			String sql = "update tbl_user set name=?, email=?, remark=?, is_admin=?, is_developer=?, is_configer=? where id=?";
			ps = conn.prepareStatement(sql);
			ps.setString(1, appTblUser.getName());
			ps.setString(2, appTblUser.getEmail());
			ps.setString(3, appTblUser.getRemark());
			ps.setString(4, appTblUser.getIs_admin());
			ps.setString(5, appTblUser.getIs_developer());
			ps.setString(6, appTblUser.getIs_configer());
			ps.setString(7, appTblUser.getId());
			flag = ps.executeUpdate();
		} catch (SQLException e) {
			throw new SQLException("update tbl_user WithKeys is Wrong!"
					+ e.getMessage());
		} finally {
			close1(conn, ps);
		}
		return flag;
	}

	/**
	 * 删除模块参数
	 * 
	 * @param appModuleParam
	 * @return
	 * @throws SQLException
	 * @throws ClassNotFoundException
	 */
	public int delete(String recordKey) throws SQLException,
			ClassNotFoundException {
		Connection conn = ConnectionManager.getConnection();
		PreparedStatement ps = null;
		int flag;
		try {
			String sql = "delete from tbl_user where id=?";
			ps = conn.prepareStatement(sql);
			ps.setString(1, recordKey);
			flag = ps.executeUpdate();
		} catch (SQLException e) {
			throw new SQLException("delete tbl_user WithKeys is Wrong!"
					+ e.getMessage());
		} finally {
			close1(conn, ps);
		}

		return flag;
	}
	
	/**
	 * 修改密码
	 * 
	 * @param
	 * @return
	 * @throws SQLException
	 * @throws ClassNotFoundException
	 */

	public int updateUserPwd(AppTblUser appTblUser) throws SQLException,
			ClassNotFoundException {
		Connection conn = ConnectionManager.getConnection();
		PreparedStatement ps = null;
		int flag;
		try {
			String sql = "update tbl_user set password=? where id=?";
			ps = conn.prepareStatement(sql);
			ps.setString(1, appTblUser.getPassword());
			ps.setString(2, appTblUser.getId());
			flag = ps.executeUpdate();
		} catch (SQLException e) {
			throw new SQLException("update tbl_user password WithKeys is Wrong!"
					+ e.getMessage());
		} finally {
			close1(conn, ps);
		}
		return flag;
	}

	/**
	 * 关闭资源
	 * 
	 * @param theRs
	 * @param theStmt
	 * @param thePs
	 */
	protected void close(ResultSet theRs, Statement theStmt,
			PreparedStatement thePs) {
		try {
			if (theRs != null)
				theRs.close();
			if (theStmt != null)
				theStmt.close();
			if (thePs != null)
				thePs.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 关闭资源
	 * 
	 * @param theRs
	 * @param theStmt
	 * @param thePs
	 */
	protected void close1(Connection conn, PreparedStatement thePs) {
		try {
			if (conn != null)
				conn.close();
			if (thePs != null)
				thePs.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 关闭资源
	 * 
	 * @param theRs
	 * @param theStmt
	 * @param thePs
	 */
	protected void close2(ResultSet theRs, Connection conn,
			PreparedStatement thePs) {
		try {
			if (theRs != null)
				theRs.close();
			if (conn != null)
				conn.close();
			if (thePs != null)
				thePs.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

}
